package com.tongren.framework.maintabfragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.tongren.framework.R;
import com.tongren.framework.api.Api;
import com.tongren.framework.constants.ConstantValues;
import com.tongren.framework.core.BaseFragment;
import com.tongren.framework.core.BaseHeaderListFragment;
import com.tongren.framework.core.ITabFragment;
import com.tongren.framework.model.BaseModel;
import com.tongren.framework.model.BaseModelDemo;
import com.tongren.framework.model.Benefit;
import com.tongren.framework.model.News;
import com.tongren.framework.utils.L;
import com.tongren.framework.widgets.pull.BaseViewHolder;
import com.tongren.framework.widgets.pull.PullRecycler;
import com.tongren.framework.widgets.pull.layoutmanager.ILayoutManager;
import com.tongren.framework.widgets.pull.layoutmanager.MyGridLayoutManager;
import com.tongren.framework.widgets.pull.layoutmanager.MyLinearLayoutManager;
import com.tongren.framework.widgets.pull.section.SectionData;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

//import retrofit2.Retrofit;
//import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
//import retrofit2.converter.gson.GsonConverterFactory;
//import rx.Subscriber;
//import rx.android.schedulers.AndroidSchedulers;
//import rx.functions.Action1;
//import rx.schedulers.Schedulers;
//
//import static rx.internal.operators.OperatorReplay.observeOn;

/**
 * zhangxu
 */
public class HeaderListFragment extends BaseHeaderListFragment<String> implements ITabFragment {
    private int page = 1;
    private boolean headerFlag = true;
    private BaseModel results;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        enableLazyLoad();
    }

    @Override
    protected BaseViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_headerlist_item, parent, false);
        return new SampleViewHolder(view);
    }

    @Override
    protected ILayoutManager getLayoutManager() {
        return new MyLinearLayoutManager(getContext());
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    @Override
    public void setUpData() {
        if (mDataList == null) {
            recycler.setRefreshing();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(ConstantValues.KEY_SAVE_LIST, mDataList);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mDataList = (ArrayList<SectionData<String>>) savedInstanceState.getSerializable(ConstantValues.KEY_SAVE_LIST);
    }

    @Override
    public void onRefresh(final int action) {
        if (mDataList == null) {
            mDataList = new ArrayList<>();
        }
        if (action == PullRecycler.ACTION_PULL_TO_REFRESH) {
            page = 1;
        }

//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(ConstantValues.BASE_URL)
//                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                // 针对rxjava2.x
//                //.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .build();
//
//        Api api = retrofit.create(Api.class);
//        api.getNewsList()
//        .subscribeOn(Schedulers.io())
//        .observeOn(AndroidSchedulers.mainThread())
//        .subscribe(new Subscriber<BaseModel>() {
//
//            @Override
//            public void onCompleted() {
//            }
//
//            @Override
//            public void onError(Throwable e) {
//            }
//
//            @Override
//            public void onNext(BaseModel model) {
//                results = model;
//                if (action == PullRecycler.ACTION_PULL_TO_REFRESH) {
//                            mDataList.clear();
//                        }
//                        if (model.results == null ) {
//                            recycler.enableLoadMore(false);
//                        } else {
//                            recycler.enableLoadMore(true);
////                            ArrayList<News> beans = resultsModel.content.content;
////                            mDataList.addAll(beans);
//                            adapter.notifyDataSetChanged();
//                        }
//                        recycler.onRefreshCompleted();
//            }
//        });


        recycler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (action == PullRecycler.ACTION_PULL_TO_REFRESH) {
                    mDataList.clear();
                    headerFlag = true;
                }
                int size = mDataList.size();
                if(headerFlag){
                    mDataList.add(new SectionData(true, size, "header " + size));
                    headerFlag = false;
                }

                for (int i = size; i < size + 5; i++) {
                    mDataList.add(new SectionData("sample list item" + i));
                }
                adapter.notifyDataSetChanged();
                recycler.onRefreshCompleted();
                if (mDataList.size() < 50) {
                    recycler.enableLoadMore(true);
                } else {
                    recycler.enableLoadMore(false);
                }
            }
        }, 1000);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onMenuItemClick() {
        L.e("onMenuItemClick by listener");
    }

    @Override
    public BaseFragment getFragment() {
        return this;
    }

    class SampleViewHolder extends BaseViewHolder {

        TextView mSampleListItemLabel;

        public SampleViewHolder(View itemView) {
            super(itemView);
            mSampleListItemLabel = (TextView) itemView.findViewById(R.id.mSampleListItemLabel);
        }

        @Override
        public void onBindViewHolder(int position) {
            mSampleListItemLabel.setText(mDataList.get(position).t);
        }

        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(getActivity(), "点击了item"+position, Toast.LENGTH_SHORT).show();
        }

    }
}
